﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Heaven.Texturing
{
    /// <summary>
    /// Base class for texture objects
    /// </summary>
    public class Texture
    {
        #region Events

        /// <summary>
        /// Event occurs when a polygonal object has been changed
        /// </summary>
        public event EventHandler Changed;

        /// <summary>
        /// Event occurs when a texture object has 
        /// been collected by GC (in other thread!)
        /// </summary>
        public event EventHandler GarbageCollected;

        /// <summary>
        /// Raises Changed event
        /// </summary>
        protected void RaiseChangedEvent()
        {
            if (Changed != null) Changed(this, EventArgs.Empty);
        }

        #endregion

        #region Fields

        // Hash number (as a matter of 
        // fact it is just time stamp, two 
        // object can't be created
        // in equal time)
        long hash;

        #endregion

        #region Properties

        /// <summary>
        /// Gets hash number
        /// </summary>
        public long Hash
        {
            get { return hash; }
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Default constructor
        /// </summary>
        protected Texture()
        {
            // Hash number (as a matter of fact it is just 
            // time stamp, two object can't create in equal time)
            hash = System.Diagnostics.Stopwatch.GetTimestamp();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Notices that changes have been performaed
        /// </summary>
        public void Invalidate()
        {
            RaiseChangedEvent();
        }

        #endregion

        #region Destructor

        ~Texture()
        {
            if (GarbageCollected != null) GarbageCollected(this, EventArgs.Empty);
        }

        #endregion
    }
}
